/*
 * RuntimeVarHistory.java
 * 
 * last update: 16.01.2010 by Stefan Saru
 * 
 * author:	Alec(panovici@elcom.pub.ro)
 * 
 * Obs:
 */


package engine;

import middle.*;

@SuppressWarnings("serial")
class RuntimeVarHistory implements VariableHistory{

	static final int chunkIncrement = 2;

	int type;
	String history[], lastString;
	int lastIndex;

	RuntimeVarHistory(int type, int length){
		this.type = type;
		lastString = "cucu"; //'u' is not a valid digit!
		length -= length % chunkIncrement;
		history = new String [Math.max(chunkIncrement, length)];
		lastIndex = -1;
	}

	public String getStringValue(long timeUnit){
		if((int)timeUnit <= lastIndex)
			return history[(int)timeUnit];
		else
			return "X";
	}

	public void addString(String s){
		xConsole.debug(toString() + "addString: " + s + " length: " + s.length());
		if(lastIndex % chunkIncrement == 0){
			String [] newHistory = new String [history.length+chunkIncrement];
			System.arraycopy(history, 0,  newHistory, 0, history.length);
			history = newHistory;
		}

		if(lastString == null) lastString = s;
		if(!lastString.equals(s))
			lastString = s;
		history[++lastIndex] = lastString;
	}

	public int getType(){
		return type;
	}

	public String toString(){
		String s = "RuntimeVarHistory: type" + type + "\nlast index: " +
		lastIndex + "\ncontents: \n";
		for(int i = 0; i < history.length ; i++)s += i + " > " + history[i] + "\n";
		return s;
	}
}











